package courseData;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

import courseManageDataService.CourseManageDataService;

import message.Message;



import data.course;
import factory.CourseFactory;
import PO.CoursePO;
import PO.UserPO;

public class CourseDataManageController extends UnicastRemoteObject implements CourseManageDataService {

	CourseDataManage cdm;
	CourseFactory cf;
	public CourseDataManageController() throws RemoteException {
		// TODO Auto-generated constructor stub
		cdm = new CourseDataManage();
		cf = new CourseFactory();
	}

	@Override
	public Message newCourse(CoursePO course) {
		// TODO Auto-generated method stub
		course c = cf.courseMaker(course);
		
		Message m1 = cdm.queryTeacher(c.getTeacher());
		if(!m1.getIsSuccess())
			return m1;
		
		if(c.isMust())
			return cdm.addRequiredCourse( c);
		
		return cdm.addCourse(c);
	}

	@Override
	public Message update(CoursePO course) {
		// TODO Auto-generated method stub
		course c = cf.courseMaker(course);
		return cdm.updateCourse(c);
	}

	@Override
	public Message delete(CoursePO course) {
		// TODO Auto-generated method stub
		String courseID = course.getCourseNO();
		return cdm.deleteCourse(courseID);
	}
	
	@Override
	public ArrayList<CoursePO> find(UserPO po) {
		// TODO Auto-generated method stub
		ArrayList<course> list = null;
		ArrayList<CoursePO> result = new ArrayList<CoursePO>();
		int role = Integer.valueOf(po.getRole());
		if(role==4)
			list = cdm.findCourseBySid(po.getId());
		else{
			list = cdm.findCourseByTid(po.getId());
		}
			
		
		for(int i = 0;i<list.size();i++){
			CoursePO c = cf.coursePOMaker(list.get(i));
			result.add(c);
		}
		return result;
	}

	@Override
	public CoursePO findSingle(String courseNO) throws RemoteException {
		// TODO Auto-generated method stub
		 course c = cdm.findSingle(courseNO);
		 return cf.coursePOMaker(c);
	}

	@Override
	public ArrayList<CoursePO> findByDepart(String depart) throws RemoteException {
		// TODO Auto-generated method stub
		ArrayList<CoursePO> result = new ArrayList<CoursePO>();
		ArrayList<course> list = cdm.findByDepart(depart);
		for(int i = 0;i<list.size();i++){
			result.add(cf.coursePOMaker(list.get(i)));
		}
		return result;
	}

	@Override
	public ArrayList<CoursePO> find(String type) throws RemoteException {
		// TODO Auto-generated method stub
		ArrayList<CoursePO> result = new ArrayList<CoursePO>();
		ArrayList<course> list = cdm.findCourseByType(type);
		for(int i = 0;i<list.size();i++){
			result.add(cf.coursePOMaker(list.get(i)));
		}
		return result;
	}

}
